const router                     = require('koa-router')()
const {SuccessModel, ErrorModel} = require('../utils/resModel')

router.prefix('/bone')
const {
    newData,
    allList,
    destroy,
    revise
} = require('../controller/boneController')

router.get('/list', async (ctx, next) => {
    let data = await allList(ctx)

    if (Array.isArray(data)) {
        ctx.body = new SuccessModel(data);
    }else {
        ctx.body = new ErrorModel(data)
    }
})
router.post('/new', async (ctx, next) => {
    const param = ctx.request.body
    let data = await newData(ctx,param)
    if (data) {
        ctx.body = new SuccessModel(data);
    }else {
        ctx.body = new ErrorModel(data)
    }
})
router.get('/delete', async (ctx, next) => {
    let query = ctx.query
    if (query.id <= 0) {
        return ctx.body = new ErrorModel('无需删除')
    }
    let data = await destroy(ctx,query);
    if (data) {
        ctx.body = new SuccessModel(data)
    }else {
        ctx.body = new ErrorModel(data)
    }
})
router.post('/revise', async (ctx, next) => {
    const param = ctx.request.body
    let data = await revise(ctx,param)
    if (data) {
        ctx.body = new SuccessModel(data)
    }else {
        ctx.body = new ErrorModel(data)
    }
})
router.get('/id', async (ctx, next) => {
    let id = req.params.id
    let data = await models.bones.findByPk(id)
    ctx.body = new SuccessModel(data)
})

module.exports = router

